<template>
    <div>
        <div class="search-con search-con-top">
            <div>
                驾校名字：
                <Input v-model="searchData.name" style="width: 150px"></Input>
                城市：
                <Select v-model="searchData.city_id" filterable style="width: 150px">
                    <Option :value="val.city_id" v-for="val in cityArr" :key="val.id">{{ val.name }}</Option>
                </Select>
                是否推荐：
                <Select v-model="searchData.is_recommend" style="width: 150px">
                    <Option :value="0">不推荐</Option>
                    <Option :value="1">推荐</Option>
                </Select>

                <Button @click="search" type="primary" icon="ios-search" style="margin-left: 10px;">搜索</Button>
                <Button @click="reset" type="warning" icon="ios-refresh" style="margin-left: 10px;">刷新</Button>
                <Button slot="extra" @click="edit(false, false)" type="success" style="margin-left: 10px;">添加</Button>
            </div>
        </div>
        <br>
        <Table :columns="columns" :data="list" border stripe size="small">
            <template slot-scope="{ row , index}" slot="imgSlot">
                <show-img :imgUrl="row.img"></show-img>
            </template>
            <template slot-scope="{ row , index}" slot="imgSlot1">
                <show-img :imgUrl="row.bus_img"></show-img>
            </template>
            <template slot-scope="{ row , index}" slot="imgSlot2">
                <show-img :imgUrl="row.way_img"></show-img>
            </template>
            <template slot-scope="{ row , index}" slot="imgSlot3">
                <show-img :imgUrl="row.record_img"></show-img>
            </template>
            <template slot-scope="{ row , index}" slot="imgSlot4">
                <show-img :imgUrl="row.front_img"></show-img>
            </template>
            <template slot-scope="{ row , index}" slot="imgSlot5">
                <show-img :imgUrl="row.back_img"></show-img>
            </template>
            <template slot-scope="{ row , index}" slot="makeSlot">
                <Button type="info" @click="edit(row, false)" size="small" style="margin-left: 1px;">编辑</Button>
                <Button type="error" @click="del(row.id)" style="margin-left: 1px;" size="small">删除</Button>
            </template>
        </Table>
        <br>

        <Paginate :total="total" @on-change="changePage" @on-page-size-change="changeSize" :current="searchData.page">
        </Paginate>


        <Modal title="驾校信息" width="40" v-model="showAdd" :footer-hide="formItem.is_detail" :mask-closable="false">
            <Form ref="formValidate" :model="formItem" :label-width="90">
                <FormItem label="名称">
                    <Input v-model="formItem.name" placeholder=""></Input>
                </FormItem>
                <FormItem label="门头照">
                    <MyUpload :maxNumber="1" @getImages="getImg" :defaultImags="[formItem.img]"></MyUpload>
                </FormItem>
                <FormItem label="联系电话">
                    <Input v-model="formItem.tel" placeholder="" />
                </FormItem>
                <FormItem label="所在城市">
                    <Cascader :data="regionList" v-model="formItem.city"></Cascader>
                </FormItem>
                <FormItem label="详细地址">
                    <Input v-model="formItem.address" placeholder="" />
                </FormItem>
                <FormItem label="报名人数">
                    <Input v-model="formItem.num" type="number" />
                </FormItem>
                <FormItem label="是否推荐">
                    <radioGroup v-model="formItem.is_recommend">
                        <Radio :label="0">不推荐</Radio>
                        <Radio :label="1">推荐</Radio>
                    </radioGroup>
                </FormItem>
                <FormItem label="推荐原因" v-if="formItem.is_recommend == 1">
                    <Input v-model="formItem.recommend_reason" placeholder="" />
                </FormItem>
                <FormItem label="营业执照">
                    <Row>
                        <MyUpload :maxNumber="1" @getImages="getImg1" :defaultImags="[formItem.bus_img]"></MyUpload>
                        道路运输许可证/备案表：
                        <MyUpload :maxNumber="1" @getImages="getImg2" :defaultImags="[formItem.way_img]"></MyUpload>
                    </Row>
                </FormItem>
                <FormItem label="身份证正面">
                    <Row>
                        <MyUpload :maxNumber="1" @getImages="getImg4" :defaultImags="[formItem.front_img]"></MyUpload>
                        身份证反面：
                        <MyUpload :maxNumber="1" @getImages="getImg5" :defaultImags="[formItem.back_img]"></MyUpload>
                    </Row>
                </FormItem>
            </Form>
            <div slot="footer">
                <Button type="primary" size="large" long @click="editModalOk">确定</Button>
            </div>
        </Modal>
    </div>
</template>

<script>
import Paginate from "_c/paginate";
import ShowImg from '_c/show_img'
import MyUpload from "_c/my-upload"
import regionList from "../../libs/regions.json";
export default {
    name: "schoolList",
    components: {
        Paginate, ShowImg, MyUpload
    },
    data() {
        return {
            searchData: {
                page: 1,
                limit: 10
            },
            // 列表
            columns: [
                {
                    title: '城市',
                    key: 'city_name',
                    align: "center"
                },
                {
                    title: '名称',
                    key: 'name',
                    align: "center"
                },
                {
                    title: '门头照',
                    key: 'img',
                    align: "center",
                    slot: "imgSlot"
                },
                {
                    title: '报名人数',
                    key: 'num',
                    align: "center"
                },
                {
                    title: '成功人数',
                    key: 'success_num',
                    align: "center"
                },
                {
                    title: '取消人数',
                    key: 'cancel_num',
                    align: "center"
                },
                {
                    title: '联系电话',
                    key: 'tel',
                    align: "center"
                },
                {
                    title: '地址',
                    key: 'address',
                    align: "center"
                },
                {
                    title: '营业执照',
                    key: 'bus_img',
                    align: "center",
                    slot: "imgSlot1"
                },
                {
                    title: '道路运输许可证',
                    key: 'way_img',
                    align: "center",
                    slot: "imgSlot2"
                },
                {
                    title: '身份证正面',
                    key: 'front_img',
                    align: "center",
                    slot: "imgSlot4"
                },
                {
                    title: '身份证反面',
                    key: 'back_img',
                    align: "center",
                    slot: "imgSlot5"
                },
                // {
                //     title: '转化率',
                //     key: 'ratio',
                //     align: "center"
                // },
                // {
                //     title: '是否推荐',
                //     key: 'is_recommend',
                //     align: "center",
                //     render: (h, param) => {
                //         let txt = '';
                //         if (param.row.is_recommend == 0) {
                //             txt = '不推荐';
                //         }
                //         if (param.row.is_recommend == 1) {
                //             txt = '推荐';
                //         }
                //         return h('div', txt);
                //     }
                // },
                // {
                //     title: '推荐原因',
                //     key: 'recommend_reason',
                //     align: "center"
                // },
                // {
                //     title: '创建时间',
                //     key: 'create_time',
                //     align: "center"
                // },
                {
                    title: '操作',
                    align: "center",
                    slot: 'makeSlot',
                },
            ],
            // 数据
            list: [],
            total: 0,
            formItem: {},
            showAdd: false,
            baseList: [],
            regionList: regionList,
            cityArr: [],
        }
    },
    methods: {
        getImg(items) {
            items.forEach(item => {
                this.formItem.img = item.url;
            });
        },
        getImg1(items) {
            items.forEach(item => {
                this.formItem.bus_img = item.url;
            });
        },
        getImg2(items) {
            items.forEach(item => {
                this.formItem.way_img = item.url;
            });
        },
        getImg3(items) {
            items.forEach(item => {
                this.formItem.record_img = item.url;
            });
        },
        getImg4(items) {
            items.forEach(item => {
                this.formItem.front_img = item.url;
            });
        },
        getImg5(items) {
            items.forEach(item => {
                this.formItem.back_img = item.url;
            });
        },
        // 查询管理员信息
        getList() {
            this.$axios('/admin/school/school/getList', this.searchData).then(res => {
                this.list = res.data;
                this.total = res.total;
            });
        },
        search() {
            this.searchData.page = 1;
            this.getList();
        },
        reset() {
            this.searchData = {
                page: 1,
                limit: 10,
            };
            this.getList();
        },
        changePage(page) {
            this.searchData.page = page;
            this.getList();
        },
        changeSize(size) {
            this.searchData.limit = size;
            this.getList();
        },
        customSort(column, key, order) {
            this.searchData.order_by = column.key;
            this.searchData.sort = column.order;
            this.getList();
        },
        editModalOk() {
            this.$axios('/admin/school/school/save', this.formItem).then(res => {
                this.$Message.success('添加成功');
                this.showAdd = false;
                this.formItem = {};
                this.getList();
            });
        },
        edit(row, isDetail) {
            this.getBaseArr();
            if (row) {
                this.formItem = { ...row };
                if (this.formItem.is_recommend > 0) {
                    this.formItem.is_recommend = 1;
                }
            } else {
                this.formItem = {
                    id: 0,
                    name: "",
                    tel: "",
                    is_recommend: 0,
                }
            }
            this.showAdd = true;
        },
        del(id) {
            this.$Modal.confirm({
                title: '删除',
                content: "<p>您确定要删除吗？</p>",
                onOk: () => {
                    this.$axios('/admin/school/school/del', { id: id }).then(res => {
                        this.$Message.success('删除成功');
                        this.getList();
                    });
                },
                onCancel: () => {
                    this.$Message.info('取消操作');
                }
            });
        },
        getBaseArr() {
            this.$axios('/admin/school/base/getArr').then(res => {
                this.baseList = res;
            });
        },
        openCityArr() {
            this.$axios('/admin/school/base/getArr').then(res => {
                this.cityArr = res;
            });
        },
    },
    created() {
        this.getList();
        this.openCityArr();
    }
}
</script>
